package com.hollycrm.hollysqm.handle.service.v8.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.hollycrm.hollysqm.entities.TblQmPlanParam;
import com.hollycrm.hollysqm.handle.service.PlanChain;

/**
 * 质检数据查询（本类是不走Solr，直接走表查询获取数据）
 * @author jianglong
 * @date 2017年3月22日 下午3:54:48
 */
@Service("paperService")
public class PaperServiceImpl extends PlanChain {

	private static final Logger log = LoggerFactory.getLogger(PaperServiceImpl.class);
	
	@Autowired
    private JdbcTemplate jdbc;
	
	/**
	 * 主方法
	 */
	@Override
	public String applyRule(TblQmPlanParam bean, Map<String, Object> paramMap) throws Exception {
		log.info("计划抽取 >> 接触记录数据查询 过滤 ...");
		StringBuffer sql = new StringBuffer();
		sql.append("insert into tbl_qm_plan_extract (id,plan_id,data_id)");
		sql.append(" select sys_guid() as id,'").append(bean.getPlanId()).append("' as plan_id");
		sql.append(",custcontinfo_id from tbl_voc_custcontinfo where 1=1 ");
		String startTime =(String) paramMap.get("startTime");
		String endTime =(String) paramMap.get("endTime");
		if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){//来话日期
			sql.append(" and day>='").append(startTime).append("' and day <='").append(endTime).append("'");
		}
		String satisfication =(String) paramMap.get("satisfication");
		if (StringUtils.isNotBlank(satisfication)){ //满意度
			sql.append(" and satisfication in (").append(satisfication).append(")");
		}
		String recoinfoLength =(String) paramMap.get("recoinfoLength");//通话时长
		if (StringUtils.isNotBlank(recoinfoLength) && recoinfoLength.indexOf("-")!=-1){
			String [] recoinfos =  recoinfoLength.split("-");
			sql.append(" and recoinfo_length>").append(recoinfos[0]).append(" and recoinfo_length<").append(recoinfos[1]);
		}
		String custLevel =(String) paramMap.get("custLevel");
		if (StringUtils.isNotBlank(custLevel)){//用户等级
			sql.append(" and cust_level in (").append(custLevel).append(")");
		}
		String custBand =(String) paramMap.get("custBand");
		if (StringUtils.isNotBlank(custBand)){//用户品牌
			sql.append(" and cust_band in (").append(custBand).append(")");
		}
		String custArea =(String) paramMap.get("custArea");
		if (StringUtils.isNotBlank(custArea)){//号码归属地市
			sql.append(" and cust_area in (").append(custArea).append(")");
		}
		String businessType =(String) paramMap.get("businessType");
		if (StringUtils.isNotBlank(businessType)){//业务类型
			sql.append(" and business_type in (").append(businessType).append(")");
		}
		//示例
//		sql = "select * from tbl_voc_custcontinfo where" 
//				+ " satisfication in ('02','03','04','05') and "
//				+ " day>='20170301' and day <='20170331' and recoinfo_length>30 and recoinfo_length<150 and "
//				+ " cust_level in ('400','500','600') and cust_band in ('02','03','04','05') and " 
//				+ " cust_area in ('710','711','719','720','711','718') and " 
//				+ " business_type in ('03','04','05')";
		log.info("抽取SQL："+sql.toString());
		jdbc.update(sql.toString());
		return null;
	}

}
